<template>
  <div class="question-component">
    <div class="answer-view">
      <div class="content-wrapper">
        <!-- <div class="left-content" v-if="examStore.currQuestion.materials">
          <div class="content-box" v-html="examStore.currQuestion.materials"></div>
        </div> -->
        <MaterialORnot />

        <askStatus v-if="examStore.currQuestion.showAnswer" :is-correct="true" />
        <div class="right-box-list">
          <!-- 题目内容 -->
          <div class="question-content">
            <div class="quesition-list">
              <div class="question-title">
                <span v-html="mathHTMLToShow(examStore.currQuestion.title)"></span>
              </div>
            </div>

            <div class="quantity a-l_c j-c_s-a" v-if="examStore.currQuestion.type2 == 28">
              <div class="quantity-a column a-l_c">
                <div class="top">Quantity A</div>
                <div class="line"></div>
                <div
                  class="bottom"
                  v-html="changeHTML(mathHTMLToShow(examStore.currQuestion.qa))"
                ></div>
              </div>
              <div class="quantity-b column a-l_c">
                <div class="top">Quantity B</div>
                <div class="line"></div>
                <div
                  class="bottom"
                  v-html="changeHTML(mathHTMLToShow(examStore.currQuestion.qb))"
                ></div>
              </div>
            </div>

            <!-- 选项列表 -->
            <div class="radio-group" :class="{ 'radio-group-disabled': isAnalysis }">
              <!-- @click="option.is_check = !option.is_check" -->
              <div
                v-for="(option, index) in examStore.currQuestion.answerList"
                :key="option.id"
                class="option-item"
                :class="{
                  'radio-ask': !examStore.currQuestion.showAnswer && option.is_check, //正常界面
                  'radio-correct': examStore.currQuestion.showAnswer && option.key,
                  'radio-wrong':
                    examStore.currQuestion.showAnswer && !option.key && option.is_check,
                }"
                @click="handleAnswerSelect(option, index)"
              >
                <div class="radio-boxs">
                  <span class="radio-icon"></span>
                </div>
                <span
                  class="radio-label"
                  v-html="changeHTML(mathHTMLToShow(option.value))"
                ></span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import askStatus from "./askStatus.vue";
import { changeHTML } from "@/utils/index";
import MaterialORnot from "./materialORnot.vue";
import { useExam } from "@/stores/exam.js";
const examStore = useExam();
// 01 Verbal-单选题    CR题
// 01 PS-单选    PS题
// 01 DI-DS-单选题    DS题
// 01 PS-单选    PS题
// 01 EA-SC     EA-SC

import { ref, defineProps, defineEmits } from "vue";

// const selectCurrent=ref(
// 定义事件
const emit = defineEmits(["answer-selected"]);

// 定义组件属性
const props = defineProps({
  question: {
    type: Object,
    required: true,
  },
  correct: {
    type: Boolean,
    default: true,
  },
  isAnalysis: {
    type: Boolean,
    default: false,
  },
});

const recordAnswer = () => {
  // 正确答案处理
  examStore.currQuestion.is_right = examStore.examInfo.section[
    examStore.currQuestion.sectionIndex
  ].question[examStore.currQuestion.index].is_right =
    examStore.currQuestion.answerList.find((i) => i.key == 1)?.id ==
    examStore.currQuestion.answerList.find((i) => i.is_check == 1)?.id
      ? 1
      : 0;

  // 选择答案处理
  examStore.currQuestion.answer = examStore.currQuestion.answerList.find(
    (i) => i.is_check == 1
  )?.id;

  examStore.examInfo.section[examStore.currQuestion.sectionIndex].question[
    examStore.currQuestion.index
  ].is_right;
};

// 处理答案选择的函数
const handleAnswerSelect = (option, index) => {
  if (examStore.currQuestion.showAnswer) {
    return;
  }

  examStore.currQuestion.answerList.forEach((it) => (it.is_check = 0));
  examStore.currQuestion.answerList[index].is_check = 1;

  recordAnswer();
};

watch(
  () => examStore.currQuestion,
  (val) => {
    recordAnswer();
  },
  { deep: true, immediate: true }
);
</script>

<style scoped lang="scss">
.question-component {
  padding: 20px;
  border-radius: 8px;
  background-color: #fff;
}

.quantity {
  width: 100%;
  height: 80px;

  .quantity-a,
  .quantity-b {
    .top {
      padding: 0 10px;
    }

    .line {
      width: 100%;
      height: 1px;
      background-color: #000;
      margin: 4px 0;
    }

    .bottom {
      font-size: 14px;
      // padding-top: 10px;

      // span.active{
      //   border-top: 1px solid #000;
      // }
    }
  }
}

.answer-view {
  width: 100%;
}
.content-wrapper {
  display: flex;
  .left-content,
  .right-box-list {
    flex: 1;
  }

  .left-content {
    padding-right: 10px;
    border-right: 1px solid #ccc;
    margin-right: 10px;
  }
}

// Radio group styles
.radio-group {
  display: block;

  &.radio-group-disabled {
    // opacity: 0.6;
    pointer-events: none;
  }
}

.option-item {
  display: flex;
  align-items: center;
  // margin: 12px 0;
  padding: 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;

  &:hover {
    background-color: #f5f7fa;
  }

  &.radio-correct {
    .radio-boxs {
      border-color: #1e8fda;
    }

    .radio-icon {
      border-color: #1e8fda;
      background-color: #1e8fda;
    }
  }

  &.radio-wrong {
    .radio-boxs {
      border-color: #e52121;
    }

    .radio-icon {
      border-color: #e52121;
      background-color: #e52121;
    }
  }

  &.radio-ask {
    .radio-boxs {
      border-color: #126983;
    }

    .radio-icon {
      border-color: #126983;
      background-color: #126983;
    }
  }

  &.radio-selected {
    .radio-boxs {
      border-color: rgba(30, 143, 218, 0.4);
    }

    .radio-icon {
      border-color: rgba(30, 143, 218, 0.4);
      background-color: rgba(30, 143, 218, 0.4);
    }
  }
}

.radio-boxs {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background-color: #fff;
  border: 1px solid #cccccc;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
  flex-shrink: 0;

  .radio-icon {
    width: 16px;
    display: block;
    height: 16px;
    // margin-right: 8px;
    border: 2px solid #ffff;
    border-radius: 50%;
    transition: all 0.3s;
  }
}

.radio-label {
  font-size: 14px;
}

.question-title {
  margin-bottom: 20px;
  font-size: 16px;
  line-height: 1.5;
}

.question-type {
  font-weight: bold;
  color: #333;
}

/* Radio 样式调整 */
:deep(.el-radio) {
  width: 100%;
  margin-right: 0;
}

:deep(.el-radio__label) {
  white-space: normal; // 标签文本换行
}

/* 新增的 Radio 状态样式 */
.radio-correct {
  // background-color: #1E8FDA; /* 正确答案的背景色 */
  :deep(.el-radio__input.is-checked .el-radio__inner) {
    border-color: #1e8fda;
    background-color: #1e8fda;
  }
}

.radio-wrong {
  // background-color: #E52121; /* 错误答案的背景色 */
  :deep(.el-radio__input.is-checked .el-radio__inner) {
    border-color: #e52121;
    background-color: #e52121;
  }
}

.radio-selected {
  // background-color: #1E8FDA; /* 用户选择的背景色 */
  :deep(.el-radio__input.is-checked .el-radio__inner) {
    border-color: #1e8fda;
    background-color: #1e8fda;
  }
}

.radio-disabled {
  opacity: 0.6;
  /* 禁用状态的透明度 */
}

.quesition-list {
  display: flex;
}
</style>
